package com.yang.service.adminimpl;

import com.aliyun.oss.model.PutObjectResult;
import com.github.pagehelper.PageInfo;
import com.yang.bean.MarketCategory;
import com.yang.bean.MarketCategoryExample;
import com.yang.bean.MarketStorage;
import com.yang.bean.common.CommonData;
import com.yang.bean.yvo.CateInfo;
import com.yang.bean.yvo.Category;
import com.yang.bean.yvo.CategoryListVO;
import com.yang.config.AliyunCompoent;
import com.yang.mapper.MarketCategoryMapper;
import com.yang.mapper.MarketStorageMapper;
import com.yang.service.admin.GoodsCategoryService;
import com.yang.utils.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: 杨庆
 * @since: 2022.9.6 19:03
 * @description:
 */
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    @Autowired
    MarketStorageMapper marketStorageMapper;

    @Autowired
    AliyunCompoent aliyunCompoent;



    /**
    * @description: 获取商品的类目信息
    * @return: java.util.List<com.yang.bean.yvo.CategoryListVO>
    * @author: 杨庆
    * @time: 2022.9.6 19:24
     */

    @Override
    public CommonData selectAll() {
        CategoryListVO categoryListVO = new CategoryListVO();
        //TODO 思路2 直接根据level=L1
        MarketCategoryExample example1 = new MarketCategoryExample();
        example1.setOrderByClause("add_time desc");
        MarketCategoryExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andLevelEqualTo("L1");
        List<MarketCategory> list = marketCategoryMapper.selectByExample(example1);
        List<Category> list1 =new ArrayList<>();
        //遍历集合查找子类  遍历
        for (MarketCategory marketCategory : list) {
            Category category = new Category();
            Integer id = marketCategory.getId();
            // 根据id查找子类
            MarketCategoryExample example = new MarketCategoryExample();
            example.setOrderByClause("update_time desc");
            MarketCategoryExample.Criteria criteria = example.createCriteria();
            criteria.andPidEqualTo(id);
            //获得所有的子类
            List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(example);
            //赋值
            category.setId(marketCategory.getId());
            category.setDesc(marketCategory.getDesc());
            category.setId(marketCategory.getId());
            category.setName(marketCategory.getName());
            category.setKeywords(marketCategory.getKeywords());
            category.setIconUrl(marketCategory.getIconUrl());
            category.setPicUrl(marketCategory.getPicUrl());
            category.setLevel(marketCategory.getLevel());
            category.setChildren(marketCategories);
            list1.add(category);
        }
        PageInfo<Category> pageInfo = new PageInfo<>(list1);
        CommonData data = CommonData.data(pageInfo);
        return data;

    }

    @Override
    public CommonData selectCateList() {
        // 根据id查找子类
        List<MarketCategory> list = marketCategoryMapper.selectPidAll();
        CommonData<CateInfo> objectCommonData = new CommonData<>();
        List<CateInfo> list1 = new ArrayList<>();
        for (MarketCategory marketCategory : list) {
            CateInfo cateInfo = new CateInfo();
            cateInfo.setValue(marketCategory.getId());
            cateInfo.setLabel(marketCategory.getName());
            list1.add(cateInfo);
        }
        objectCommonData.setList(list1);
        PageInfo pageInfo =new PageInfo(list);
        objectCommonData.setTotal((int) pageInfo.getTotal());
        objectCommonData.setPage(1);
        objectCommonData.setPages(1);
        objectCommonData.setLimit(10);
        return objectCommonData;
    }

    @Override
    public int updateCategory(Category category) {
        //TODO 传过来的参数 考虑的问题，是父类信息进行了修改还是子类
        //TODO 先取出父类信息修改  在取出子类
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(category.getId());
        marketCategory.setName(category.getName());
        marketCategory.setKeywords(category.getKeywords());
        marketCategory.setDesc(category.getDesc());
        marketCategory.setIconUrl(category.getIconUrl());
        marketCategory.setPicUrl(category.getPicUrl());
        marketCategory.setLevel(category.getLevel());
        marketCategory.setUpdateTime(new Date());
        //TODO 0906的修改
        //TODO 获取pid
        marketCategory.setPid(category.getPid());
        MarketCategoryExample example = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(marketCategory.getId());
        int count = marketCategoryMapper.updateByExampleSelective(marketCategory, example);
        if (count == 1) {
            return 200;
        }
        return 404;
    }

    @Override
    public int deleteCategory(Category category) {
        //TODO 删除商品
        Integer id = category.getId();
        int count = marketCategoryMapper.deleteByPrimaryKey(id);
        List<MarketCategory> children = category.getChildren();
        if (children != null) {
            for (MarketCategory child : children) {
                Integer id1 = child.getId();
                marketCategoryMapper.deleteByPrimaryKey(id1);
            }
        }
        if (count == 1) {
            return 200;
        }
        return 404;
    }

    @Override
    public MarketStorage createStorage(MarketStorage marketStorage) {
        int count = marketStorageMapper.insertSelective(marketStorage);
        MarketStorage marketStorage1 = marketStorageMapper.selectByPrimaryKey(marketStorage.getId());
        return marketStorage1;
    }

    @Override
    public MarketCategory createCategory(MarketCategory marketCategory) {
        marketCategory.setAddTime(new Date());
        marketCategoryMapper.insertSelective(marketCategory);
        MarketCategory marketCategory1 = marketCategoryMapper.selectByPrimaryKey(marketCategory.getId());
        return marketCategory1;
    }


    /**
    * @description: oss文件上传到阿里云的oss存储
    [originalFilename]
    * @return: com.yang.bean.MarketStorage
    * @author: 杨庆
    * @time: 2022.9.10 23:42
     */
    @Override
    public MarketStorage fileupload(MultipartFile file)  {

        //TODO 规范化文件的名称

        String originalFilename = file.getOriginalFilename();
        int index = originalFilename.indexOf(".");
        String fileType = originalFilename.substring(index);

        String filename = UUID.randomUUID().toString().replaceAll("-", "")+fileType;
        try {
            PutObjectResult putObjectResult = aliyunCompoent.fileUpload(Constant.ALIYUN_FILEDIR+filename, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        //TODO 文件的属
        // TODO 上传成功需要插入到表中
        String contentType = file.getContentType();
        long size = file.getSize();
        MarketStorage marketStorage = new MarketStorage();
        marketStorage.setAddTime(new Date());
        marketStorage.setName(originalFilename);
        marketStorage.setSize((int) size);
        marketStorage.setType(contentType);
        marketStorage.setUrl(Constant.ALIYUN_URL+filename);
        marketStorage.setKey(filename);
        marketStorageMapper.insertSelective(marketStorage);
        return marketStorage;
    }
}
